'''
Created on 27/03/2011

@author: Eran_Z

Feasibility study (main)
'''

import weights_m
import scores_m

def generateWeights(algorithm, context, world):
    """Generates a list of weights for the context items.
    This is a list of numbers (preferrably positive)"""
    return algorithm(context, world)

def calculateScores(algorithm, context, weights, world):
    """Calculates the scores for each of the world items.
    In the future, it will probably use many internet searches."""
    return algorithm(context, weights, world)

def sortWorld(world, scores):
    """Sorts the world and scores lists according to the scores list,
    from largest to smallest."""
    combined = [(world[i],scores[i]) for i in range(len(world))]
    combined = sorted(combined, key=lambda t: t[1], reverse=True)
    return ([w[0] for w in combined], [s[1] for s in combined])

########################################################
########################################################

# MAIN function:
def COR_algorithm(weightingAlgorithm, scoringAlgorithm, context, world):
    #get settings
    chosenWeightingAlg = weights_m.weightingAlgorithms[weightingAlgorithm]
    chosenScoringAlg = scores_m.scoringAlgorithms[scoringAlgorithm]
    
    #First stage: generate weights
    weights = generateWeights(chosenWeightingAlg, context, world)
    
    #Second stage: calculate scores
    scores = calculateScores(chosenScoringAlg, context, weights, world)
    
    #sort world according to scores
    (world, scores) = sortWorld(world, scores)
    
    #return results
    return world


########################################################
# example invocation of the algorithm:
#context = ["Aladdin", "Cinderella", "Snow White"]
#world = ["the Exorcist", "Sex and the city", "Toy Story"]
#COR_algorithm("Mutual Information", "Normalized Mutual Information", context, world)
